﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Client
{
    public class LoginPresenter
    {
        #region Private members

        private ILoginView m_loginView;

        #endregion Private members


        #region Constructor

        public LoginPresenter(ILoginView loginView)
        {
            //set login view
            m_loginView = loginView;

            //subscribe on events
            m_loginView.LoginClick += new EventHandler(LoginView_LoginClick);
        }

        #endregion Constructor

        
        #region Private methods

        /// <summary>
        /// LoginEvent Handler. Click on Login button
        /// </summary>    
        private void LoginView_LoginClick(object sender, EventArgs e)
        {
            LoginEventArgs ea = e as LoginEventArgs;
            this.Login(ea.Login, ea.Password);
        }

        #endregion Private methods


        #region Public methods

        public void LoginClickHandler(object sender, LoginEventArgs e)
        {
            this.Login(e.Login, e.Password);
        }

        /// <summary>
        /// Process login. Send request to server
        /// </summary>
        public void Login(string login, string password)
        {
            ///create model
            LoginModel loginModel = new LoginModel();

            ///try to log in
            int loginRes = loginModel.Login(login, password);
            
            ///analyse loginRes
            if (loginRes == -1)///login or password is fail
            {
                ///show error icon
                m_loginView.ShowErrorIcon();               
            }
            else ///login ok
            {             
                ///create other view by user role
                ViewFactory.CreateView((UserRole)loginRes);

                ///close login form
                m_loginView.CloseWindow();
            }

        }

       
        #endregion Public methods
        
    }//class
}
